package com.tgy.ailiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tgy.ailiyun.dao.StudentDao;
import com.tgy.ailiyun.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("studentService")
@Slf4j
public class StudentServiceImpl {
    @Resource
    StudentDao studentDao;



    public Map<String, Object> getAll(Student student){
        Map<String, Object>  map = new HashMap<>();
        map.put("code",1);
        try{
            QueryWrapper<Student> queryWrapper = new QueryWrapper<>(student);
            List<Student> list = this.studentDao.selectList(queryWrapper);
            map.put("code",0);
            map.put("data",list);
            map.put("count",list.size());
            map.put("msg","");
        } catch (Exception e) {
            log.error(e.getMessage());
            map.put("data","error");
        }finally {
            return map;
        }
    }

    public Map<String, Object> add(Student student){
        Map<String, Object> map = new HashMap<>();
        map.put("code",1);
        try {
            int result = this.studentDao.insert(student);
            map.put("code",0);
            map.put("data",result);
            map.put("msg","");
        } catch (Exception e) {
            log.error(e.getMessage());
        }finally {
            return map;
        }
    }

    public Map<String, Object> delById(Integer id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",1);
        try {
            int result = this.studentDao.deleteById(id);;
            map.put("code",0);
            map.put("data",result);
            map.put("msg","");
        } catch (Exception e) {
            log.error(e.getMessage());
        }finally {
            return map;
        }
    }


    public HashMap<String, Object> upd (Student student){
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",1);
        try {
            int result = this.studentDao.updateById(student);
            map.put("code",0);
            map.put("data",result);
            map.put("msg","");
        } catch (Exception e) {
            log.error(e.getMessage());
        }finally {
            return map;
        }
    }
}
